package bank;

import java.io.IOException;
import java.rmi.RemoteException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.BankException;

import beans.AccountBeheerBean;
import beans.CommonBean;
import beans.PageBean;

public class AccountBeheer extends HttpServlet
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void init()
	{
		System.out.println("AccountBeheer.init()");
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("AccountBeheer.doGet()");
		AccountBeheerBean accountBean = new AccountBeheerBean();
		
		UberClass uberclass = new UberClass(request, response);
		PageBean pb = new PageBean("medewerker/AccountBeheer.jsp");
		request.setAttribute("page", pb);
		request.setAttribute("ab", accountBean);
		uberclass.startOutput();
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("AccountBeheer.doPost()");
		AccountBeheerBean accountBean = new AccountBeheerBean();
		
		UberClass uberclass = new UberClass(request, response);
		
		String command = request.getParameter( "command" );
		if(command.equals("Creeer"))
			createAccount(request, uberclass, accountBean);
		else if(command.equals("Controleer"))
			checkAccount(request, uberclass, accountBean);
		
		PageBean pb = new PageBean("medewerker/AccountBeheer.jsp");
		request.setAttribute("page", pb);
		request.setAttribute("ab", accountBean);
		uberclass.startOutput();
	}
	
	public void createAccount(HttpServletRequest r, UberClass uberclass, AccountBeheerBean accountBean)
	{
		boolean error = false;
		String name = r.getParameter( "name" );
		String pin = r.getParameter( "pin" );
		double limit = 0;
		
		try {
			limit = Double.parseDouble( r.getParameter( "limit" ) );
			if(limit < 0)
			{
				uberclass.error("Het limiet moet een positieve waarde zijn.");
			}
		}
		catch ( NumberFormatException e)
		{
			error = true;
			uberclass.error("Het limiet mag uitsluitend een decimaal getal zijn.");
		}
		if(name == "")
		{
			error = true;
			uberclass.error("U moet een naam invoeren.");
		}
		if(pin == "")
		{
			error = true;
			uberclass.error("U moet een pincode invoeren.");
		}
		if(!error)
		{
			try
			{
				String reknr = uberclass.getManager().newAccount(limit, name, pin);
				CommonBean result = new CommonBean();
				result.put( "limit", "" + limit );
				result.put( "name", name );
				result.put( "pin", pin );
				result.put( "reknr", reknr );
				r.setAttribute("result", result);
			}
			catch ( BankException e )
			{
				uberclass.error("Er zijn fouten opgetreden tijdens het verwerken van de gegevens...");
			}
		}
		accountBean.put( "includeSubPage", "yes" );
		accountBean.put( "subPage", "CreateAccount.jsp" );
	}
	
	public void checkAccount(HttpServletRequest r, UberClass uberclass, AccountBeheerBean accountBean)
	{
		String reknr = r.getParameter("reknr");
		accountBean.put( "includeSubPage", "yes" );
		accountBean.put( "subPage", "ControleerAccount.jsp" );
		String[] account = uberclass.getManager().getAccount( reknr );
		accountBean.put( "reknr", account[0] );
		accountBean.put( "saldo", account[1] );
		accountBean.put( "limiet", account[2] );
		accountBean.put( "naam", account[3] );
		accountBean.put( "pincode", account[4] );
		accountBean.put( "salt", account[5] );
		String[][] trans = uberclass.getManager().getTransactions( reknr );
		accountBean.put( "trans", trans );
		accountBean.put( "transLength", trans.length);
	}
}
